package com.siemens.industrial.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/10/10.
 */
public class JsonToOtherFormat {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static Map<String, Object> buildJsonToMap(String data) {

        Map<String, Object> map = new HashMap<>();

        // convert JSON string to Map
        try {
            map = objectMapper.readValue(data, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
            return new HashMap<>();
        }
        return map;
    }

    public static String buildJsonToString(Object data) {
        try {
            return objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static List<Map<String, Object>> buildJsonToMaps(String data) {
        try {
           return (List<Map<String,Object>>)objectMapper.readValue(data,List.class);

        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<Map<String, Object>>();
        }
    }

    public static List<Integer> buildStringToList(String data) {
        try {
           return (List<Integer>)objectMapper.readValue(data,List.class);

        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<Integer>();
        }
    }
}
